package edu.ssdut.revise.api;

import edu.ssdut.revise.core.match.CUMatcher;
import edu.ssdut.revise.core.match.MatchStore;
import edu.ssdut.revise.exceptions.*;
import edu.ssdut.revise.models.CUWithInfo;
import edu.ssdut.revise.models.VisFragment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

import static edu.ssdut.revise.utils.jGitUtils.gitInput;

public class RefactoringVis {
    private static final Logger logger = LoggerFactory.getLogger(RefactoringVis.class);

    /**
     * api to get fragment with git information
     *
     * @param repoIdentifier identifier of the git repo, local path or remote url
     * @param startCommitId  starting commit hash
     * @param endCommitId    ending commit hash
     * @param ifLocal        if the identifier is local path
     * @return {@link VisFragment} or <b>null</b> if inputs are illegal
     * @throws NotExistOrNotDirectoryException repo path does not exist or is no path of directory
     */
    public VisFragment getFragmentWithGit(String repoIdentifier, String startCommitId, String endCommitId, Boolean ifLocal)
            throws NotExistOrNotDirectoryException, GitOperationException, NotLegalRepositoryException, IOException, IllegalCommitHashException, ParserException {
        // get MatchStore
        MatchStore<CUWithInfo> cuWithInfoMatchStore = gitInput(repoIdentifier, startCommitId, endCommitId, ifLocal);
        // get fragment
        return new VisFragment(repoIdentifier, startCommitId, endCommitId, cuWithInfoMatchStore);
    }

    /**
     * api to get fragment with directory information
     *
     * @param dir1 base directory path
     * @param dir2 modified directory path
     * @return {@link VisFragment} or <b>null</b> if inputs are illegal
     * @throws NotExistOrNotDirectoryException directory provided does not exist or is no directory.
     */
    public VisFragment getFragment(String dir1, String dir2) throws NotExistOrNotDirectoryException, ParserException {
        // get 2 directories
        CUMatcher cuMatcher = new CUMatcher(dir1, dir2);
        // get fragment
        return new VisFragment(dir1, dir2, cuMatcher.getCuMatchStore());
    }
}
